package com.yjzx.util.common.util.BeanUtils;

import com.yjzx.util.common.util.TimeUtils;

import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yjzx
 * @date 2024/5/31
 * @description BeanUtils
 */
public class BeanUtils {
    @SafeVarargs
    public static <T> List<Object> toList(BaseInterface<T> baseClass, T t, LambdaFunction<T, ?>... excludeColumn) {
        Class<T> genericityClass = baseClass.getGenericityClass();

        Set<String> set =Arrays.stream(excludeColumn).map(lambdaFunction->lambdaFunction.getLambdaColumn().getName()).collect(Collectors.toSet());
        return toList(genericityClass,t,set);
    }

    public static <T> List<Object> toList(Class<T> clazz, T t,Set<String> columnName) {
        List<Object> list = new LinkedList<>();
        try {
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if (!columnName.contains(field.getName())) {
                    Object o = field.get(t);
                    if (o instanceof Date) {
                        list.add(TimeUtils.sdf_ymd.format((Date) o));
                    } else {
                        list.add(o);
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return list;
    }
    @SafeVarargs
    public static <T> Map<String,Object> toMap(BaseInterface<T> baseClass, T t, LambdaFunction<T, ?>... excludeColumn) {
        Class<T> genericityClass = baseClass.getGenericityClass();

        Set<String> set =Arrays.stream(excludeColumn)
                .map(lambdaFunction->lambdaFunction.getLambdaColumn().getName()).collect(Collectors.toSet());
        return toMap(genericityClass,t,set);
    }

    public static <T> Map<String,Object> toMap(Class<T> clazz, T t, Set<String> columnName) {
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        try {
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if (!columnName.contains(field.getName())) {
                    Object o = field.get(t);
                    if (o instanceof Date) {
                        o = TimeUtils.sdf_ymd.format((Date) o);
                    }
                    map.put(field.getName(),o);
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return map;
    }
}
